<template>
  <view>
    <view class="select" v-if="JSON.stringify(address)=='{}'">
      <button type="primary" size='mini' class="selectBtn" @click="chooseAddress">请选择收获地址+</button>
    </view>
    
    <view class="address" v-else>
      <view class="row1">
        <view class="left">
          收货人：{{address.userName}}
        </view>
        <view class="right" @click="chooseAddress">
          电话：{{address.telNumber}}
          <uni-icons type="arrowright" size="16" ></uni-icons>
        </view>
      </view>
      <view class="row2">
        收获地址：{{addressAll}}
      </view>
    </view>
    
    <!-- 底部的边框线 -->
    <image src="../../static/cart_border@2x.png" mode="" class="addressBorder"></image>
  </view>
</template>

<script>
  import {mapMutations,mapState,mapGetters} from 'vuex'
  export default {
    name:"my-address",
    computed:{
      ...mapState('user',['address']),
      ...mapGetters('user',['addressAll'])
    },
    data() {
      return {
        
      };
    },
    methods:{
      async chooseAddress(){
        const [err,succ]=await uni.chooseAddress().catch(err=>err);
        // 如果用户没有授权
        if(err&&(err.errMsg==='chooseAddress:fail auth deny'||err.errMsg==='chooseAddress:fail authorize no response')){
          // 需要重新授权
          this.reAuth();
        }
        if(err===null&&succ.errMsg==='chooseAddress:ok'){
          this.UPDATEADDRESS(succ);
        }
      },
      async reAuth(){
        const [err2,confrimResult]=await uni.showModal({
          content:'检测到您没打开地址权限，是否去设置打开?',
          confirmText:'确认',
          cancelText:"取消",
        })
        if(err2) return 
        if(confrimResult.cancel){
          return uni.$showMsg('你取消了地址授权');
        }     
        if(confrimResult.confirm){
          return uni.openSetting({
            success(settingResult) {
              if(!settingResult.authSetting['scope.address']){
                uni.$showMsg('你取消了地址授权');
              }else{
                uni.$showMsg('授权成功，请选择收货地址');           
              }
            }
          })
        }
      },
      ...mapMutations('user',['UPDATEADDRESS'])
    }
  }
</script>

<style lang="scss" scoped>
.select{
  height: 180rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}
.address{
  font-size: 14px;
  // height: 90px;
  padding: 0 10px;
  .row1{
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
    margin-top: 10px;
    .left{
      
    }
    .right{
      
    }
  }
  .row2{
    margin-bottom: 10px;
  }
}

.addressBorder{
  display: block;
  width: 100%;
  height: 5px;
}
</style>